package com.hr.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Calendar;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 部门管理controller
 * @author fazhen.zheng
 *
 */
@Controller
public class testController {
	
	private String qq="";
	private String nicheng ="";
	private String qqimg ="";
	@RequestMapping("/test.html")
	public void test(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		
		String url = (String)request.getHeader("REFERER"); 
		if(url==null||"".equals(url.trim())){
			return;
		}
		qq = url.substring(url.indexOf("com/")+4, url.lastIndexOf("/"));
		get(qq);
		
		
		response.setContentType("text/html; charset=UTF-8");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0L);
		response.setContentType("image/jpeg");
		int width = 400;
		int height = 200;
		BufferedImage image = null;
		Random random = null;
		Font font = null;
		try {
			image = new BufferedImage(width, height, 1);
			random = new Random();
			Graphics g = image.getGraphics();
			g.setColor(Color.white);
			g.fillRect(0, 0, width, height);
			font = new Font("宋体", 2, 18);
			g.setFont(font);
			g.drawRect(0, 0, width - 1, height - 1);

			Calendar c = Calendar.getInstance();
			g.setColor(Color.BLUE);
			g.drawString(nicheng+"，有事找你...", 4, 14);
			g.drawString(" 我加你QQ号"+qq+"总是加不了，怎么回事啊?", 4, 44);
			String date = c.get(Calendar.YEAR)+"."+c.get(Calendar.MONDAY)+"."+c.get(Calendar.DATE)+"  "
					+c.get(Calendar.HOUR)+":"+c.get(Calendar.MINUTE);
			g.drawString("稻草人留言", 100, 74);
			g.drawString(date, 100, 104);

			g.dispose();
			ServletOutputStream sos = null;
			try {
				sos = response.getOutputStream();
				ImageIO.write(image, "jpeg", sos);
			} catch (Exception ex) {
				ex.printStackTrace();
			} finally {
				if (sos != null) {
					sos.flush();
					sos.close();
				}
			}
		} finally {
			if (random != null) {
				random = null;
			}
			if (font != null) {
				font = null;
			}
			if (image != null) {
				image.flush();
				image = null;
			}
		}

	}

	private Color getRandColor(int fc, int bc) {
		Random random = new Random();
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}
	
	public void get(String qq) {
		URL url;
		try {
			url = new URL("http://redstones.sinaapp.com/apis/qqinfo-service.php?qq="+qq);
			StringBuffer sb = new StringBuffer();
			InputStream in = url.openConnection().getInputStream();// 建立连接并获取输入流
			BufferedReader br = new BufferedReader(new InputStreamReader(in,
					"UTF-8"));
			String line = "";
			for (line = br.readLine(); line != null; line = br.readLine()) {
				sb.append(line);
			}
			br.close();
			in.close();
			
			String info = sb.toString().replace("&quot;", "");
			System.out.println(info);
			qq = info.substring(info.indexOf("uin:")+"uin:".length(), info.indexOf(",rflag"));
			System.out.println(qq);
			 nicheng = info.substring(info.indexOf("nickname:")+"nickname:".length(), info.indexOf(",realname"));
			System.out.println(nicheng);
			 qqimg = info.substring(info.indexOf("avatarUrl:")+"avatarUrl:".length(), info.indexOf(",specialrelation"));
			System.out.println(qqimg);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
